<?php
declare (strict_types=1);

namespace app\admin\service\content\agreement;

use app\admin\logic\content\agreement\Agreement as AgreementLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 协议管理
 */
class Agreement extends BaseService
{
    /**
     * @Inject()
     *
     * @var AgreementLogic
     */
    protected $agreementLogic;

    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @return   [type]                         [description]
     */
    public function index()
    {
        $data = $this->agreementLogic->getList([])->bindAttr('lastActionUser', ['last_user_name' => 'user_name']);

        // 定义字段
        $keyList = [
            'key'            => ListItem::make()->title('协议标识'),
            'title'          => ListItem::make('custom')->title('协议名称'),
            'last_user_name' => ListItem::make()->title('最后操作者'),
            'update_time'    => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 新建配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @return   [type]                         [description]
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = $this->agreementLogic->createAgreement($data);
            return $this->parseToData($res->toArray(), 1, '添加成功');
        }

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit();

        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/admin/content/agreement/create';

        return $form->send();
    }

    /**
     * 编辑配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function edit(int $id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = $this->agreementLogic->editAgreement($id, $data);
            return $this->parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = $this->agreementLogic->where('id', $id)->findOrFail();

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit(true, $data);
        $form->datas   = $data;

        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/content/agreement/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 删除配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        $this->agreementLogic->deleteAgreement($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 初始化字段
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     */
    protected function keyListInit($update = false, $data = null)
    {
        if ($update) {
            $keyOptions = [
                [
                    'title' => $this->agreementLogic->getAgreementTitleByKey($data->getAttr('key')),
                    'value' => $data->getAttr('key')
                ]
            ];
        } else {
            $keyOptions = $this->agreementLogic->getAgreementKeyOptions();
        }
        $keyList = [
            'key'     => FormItem::make('select')->title('协议类型')->required()->options($keyOptions)->disabled($update),
            'title'   => FormItem::make()->title('名称')->required()->maxlength(200),
            'content' => FormItem::make('editor')->title('内容')->required(),
            // 'is_system' => FormItem::make('SlideSwitch')->title('是否为系统内置')->activeValue(1)->inactiveValue(0),
            'remark'  => FormItem::make('textarea')->placeholder('可输入100字的备注说明')->title('备注')->maxlength(100)->wordLimit()->rows(4),
        ];

        return $keyList;
    }

    /**
     * 搜索协议
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-05
     *
     * @param string $keyword [description]
     * @return   [type]                                  [description]
     */
    public function search(string $keyword = '')
    {
        $list = $this->agreementLogic->getSearchList($keyword);

        return $this->parseToData($list, 1, '获取成功');

    }

    /**
     * 获取协议内容
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/7/5
     *
     * @param string $key
     * @return \think\Response
     */
    public function getAgreenmentContent(string $key)
    {
        $content = $this->agreementLogic->getContentByKey($key);

        return $this->parseToData(['content' => $content], 1, '获取成功');
    }
}
